package com.chenchao.thread;

/**
 * @encoding : UTF-8
 * @Time : 2020/11/17 20:04
 * @Author : Chen Chao
 * @File : javaSE.com.chenchao.javathread.Test13.java
 * @Software : IntelliJ IDEA
 * @since : 1.8
 * 观测线程状态
 */
public class Test13 {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                System.out.println(Thread.currentThread().getName() + "运行" + i + "次");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        Thread.State state = thread.getState();
        System.out.println(state);//NEW
        thread.start();//开启线程
        state = thread.getState();
        System.out.println(state);//RUNNABLE
        while (state != Thread.State.TERMINATED) {
            state = thread.getState();
            Thread.sleep(10);
            System.out.println(state);//TIMED_WAITING
        }//TERMINATED
    }

}
